import {ACCESS_TOKEN,REFRESH_TOKEN} from '@/utils/constant'
import { RequestRefreshToken } from '@/api/path/index'
// const requestList = [] // 保存请求
let requestObj:any = null // 如果有多次不同请求，都是token过期，我恢复请求时，只恢复最后一次过期
// 比如: token过期, 在短token刷新之前你连续了删除商品，删除bannber, 添加bannber
/**
 * 添加请求
 *  request = ()=>{ resolve( axiosInstance({url,method,data}) ) }
 */
export const addSubscriptRequest = (request:any) => {
    // requestList.push(request)
    requestObj = request
}

/**
 * 自动重新发起保存的请求
 */
export const refreshRequest = () => {
    // requestList.forEach(request => request())
    requestObj()
}

/**
 * 根据长token刷新短token
 */
export const refreshToken = () => {
    // 获取长token
    const longToken = localStorage.getItem(REFRESH_TOKEN)
    if (longToken) {
        RequestRefreshToken(longToken).then(data => {
            const { token } = data
            localStorage.setItem(ACCESS_TOKEN, token as string) // 刷新短token
            // 恢复请求
            refreshRequest()
        })
    }
}
